<template>
  <div class="search-semantic-app">
    <div class="container2">
      <div class="box">
        <!-- <h3>语义检索 或者 专业检索</h3> -->
        <div class="semantic-box">
          <el-input
            type="textarea"
            :rows="6"
            resize="none"
            :placeholder="placeholder"
            v-model="semanticTxt"
            class="semantic-inp">
          </el-input>
          <div class="search-con">
            <el-button class="search-btn" @click="search">搜索</el-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 选择数据源 -->
    <!-- <DataSource ref="dataSource"/> -->
    <!-- 字段组合 -->
    <el-dialog :visible.sync="tableShow" :title="fieldsGroup" width="990px" custom-class="fieldsGroup">
      <div class="tabs-con">
        <el-button @click="tabsFn(1)" :class="{active:tabs==1}">基础</el-button>
        <el-button @click="tabsFn(2)" :class="{active:tabs==2}">过滤</el-button>
        <el-button @click="tabsFn(3)" :class="{active:tabs==3}">高级</el-button>
        <el-button @click="tabsFn(4)" :class="{active:tabs==4}">通配符</el-button>
      </div>
      <feilds v-if="lang === 'en'" :tab="tabs" ref="feilds"></feilds>
      <feilds-cn v-else :tab="tabs" ref="feildsCn"></feilds-cn>
    </el-dialog>
  </div>
</template>

<script>
import {mapState} from 'vuex'
// import DataSource from '../DataSource'
// import Feilds from "./Fields"
// import FeildsCn from "./FieldsCn"
export default {
  props:{
    ctNavID:{
      type:String
    },
    activeIndex:{
      type:Number
    }
  },
  components: {
    // DataSource,
    // Feilds,
    // FeildsCn
  },

  data() {
    return {
      semanticTxt: '',
      tableShow: false,
      fieldsGroup: '字段组合',
      tabs: 1,
      lang: this.$cookie.get('lang'),
      ipi: 1,
      ips: 10,
      placeholder:'',
      searchps:'',
    }
  },
  computed: {
    ...mapState(['searchWord']),
  },
  watch:{
    activeIndex(val){
      if(val == 2){
        this.placeholder = '*专业检索使用字段代码（如A/、TTL/等）+ 通配符（AND、OR、NOTAND）拼成检索式，不同层级检索式可以使用括号表示，所有符号均为英文符号。例如：B/((计算机 or 程序) and (内容 or 任务))'
      }else{
        this.placeholder = '可输入检索词、语句、段落、篇章等进行检索。'
      }
    }
  },
  methods: {
    search() {
      if (this.semanticTxt === '') {
        this.$message({
          type: 'error',
          message: '检索不能为空！',
          duration: 1000
        })
        return
      }
      let sq = this.semanticTxt
      let reg = /^[A-Za-z0-9]+\//
      let v = sq.replace(/\s+/g, '').replace(/(\r\n)+/g, '') // 清除空格等字符
      if (sq.replace(/\s+/g, '')) {
        if (!reg.test(v)) {
          // 专业 不用拼
          // 语义 拼 R
          if( this.activeIndex === 3 ){
            sq =  "R/" + this.semanticTxt 
          }
        }
      }
      this.$store.commit('setSearchWord', sq)
    },
    // 选择数据源
    // openDataSource() {
    //   this.$refs.dataSource.init()
    // },
    viewTable() {
      this.lang === 'en' ? (this.fieldsGroup = 'Field Combination') : (this.fieldsGroup = '字段组合')
      this.tableShow = true;
    },
    // 上传文档
    handleUpload(params) {
      // let file = params.file
      // this.form.vcf = params.file
    },
    beforeUpload(file) {
      let arr = file.name.split('.')
      let isTxt = arr[arr.length - 1] === 'txt'
      if (!isTxt) {
        this.$message({
          type: 'warning',
          duration: 2000,
          message: '请选择txt类型的文件'
        })
      }
      this.getTxtContext(file)
      return isTxt
    },
    getTxtContext(file) {
      if (file) {
        let reader = new FileReader()
        reader.readAsText(file,'utf-8')
        reader.onload = (e) => {
          this.semanticTxt = e.target.result
        }
      }
    },
    tabsFn(i) {
      this.tabs = i;
      if (this.$refs.feilds && this.$refs.feilds.expands.length > 0) {
        this.$refs.feilds.expands = [];
      }
      if (this.$refs.feildsCn && this.$refs.feildsCn.expands.length > 0) {
        this.$refs.feildsCn.expands = [];
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.search-semantic-app {
  position: relative;
  width: 100%;
  height: auto;
  background: url(../../../../../assets/img/PS/search-bg.jpg) no-repeat center center;
  background-size: cover;
  .container2 {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin: 0 auto;
    .box {
      position: relative;
      width: 100%;
      h3 {
        width: 100%;
        line-height: 40px;
        margin: 30px auto;
        text-align: center;
        font-weight: 700;
        font-size: 18px;
        color: $color10;
      }
      .semantic-box {
        position: relative;
        // min-height: 345px;
        padding: 30px;
        border: 2px solid $mainColor !important;
        border-radius: 8px;
        background: #fff;
        .semantic-inp {
          position: relative;
          width: 100%;
          padding: 5px 5px 5px;
          border: 1px solid #dee1f2;
          box-sizing: border-box;
          background-color: #fff;
        }
        .btns {
          position: absolute;
          top: 200px;
          left: 30px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          width: calc(100% - 60px);
          height: 50px;
          line-height: 50px;
          .data-source {
            margin-left: 20px;
            color: $color41;
            cursor: pointer;
            i {
              font-size: 20px;
              &:hover {
                color: $color30;
              }
            }
          }
          .table {
            margin-right: 20px;
            font-weight: 700;
            font-size: 12px;
            color: $color30;
            user-select: none;
            cursor: pointer;
          }
        }
        .search-con{
          text-align: right;
          display: flex;
          justify-content: space-between;
          height: 30px;
          .uploader {
            display: inline-block;
          }
          .upload-btn{
            padding: 7px 25px;
            border-style: solid;
            border-width: 1px;
            border-color: $mainColor;
            color: $mainColor;
            border-radius: 15px;
            font-size: 12px;
            outline: none;
          }
          .upload-info{
            font-size: 12px;
            color: #5e5f73;
            margin-left: 20px;
          }
          .search-btn{
            position: absolute;
            right: 40px;
            bottom: 10px;
            font-size: 12px;
            padding: 8px 37px;
            background-color: $mainColor;
            color: #fff;
            font-weight: bold;
            border-radius: 15px;
            border: 1px solid $mainColor;
          }
        }
        .search-ps {
          margin: 10px 0;
          font-size: 12px;
          font-weight: bold;
          color: $color143;
          text-align: left;
          line-height: 20px;
        }
      }
    }
    
  }
}
.tabs-con {
  margin-top: 10px;
  .active {
    color: #fff;
    background-color: $color141;
  }
}
</style>